package com.ckzp.jfinal.system.controller;

import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.system.model.SysCode;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.interceptor.Roles;
import com.ckzp.jfinal.system.model.SysPage;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;

/**
 * Created by 51594 on 2020/10/28.
 */
public class SysPageController extends BaseController {

    @Admins(roles = "system", name = "", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 列表
     */
    @Roles(roles = "system")
    public void SysPageList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysPageList.html");
    }

    /**
     * 列表数据提供API
     */
    @Roles(roles = "system")
    public void SysPageListDataApi() {

        PageSearch ps = PageSearch.n()
                .setTable("sys_page")
                .initSearch(getRequest());
        Page<SysPage> pages = ps.paginate(SysPage.dao);

//        String sql = "from sys_page ";
//        sql = addSearch(sql, "page_id", "=", "string", getPara("s[pageid]"));
//        sql = addSearch(sql, "page_id", "=", "string", getPara("s[keyid]"));
//        Page<SysPage> pages = SysPage.dao.paginate(getPage(), getLimit(10), "select *", sql);
        List datalist = pages.getList();
//        for (int k = 0; k < datalist.size(); k++) {
//            SysPage st = (SysPage) datalist.get(k);
//        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加
     */
    @Roles(roles = "system")
    public void AddSysPage() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("pages", new SysPage());
        setAttr("form", fd);
        render("ModifySysPage.html");
    }

    /**
     * 修改
     */
    @Roles(roles = "system")
    public void EditSysPage() {
        String page_id = getPara("page_id", "");
        SysPage o = SysPage.dao.findById(page_id);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("form", fd);
        setAttr("pages", o);
        render("ModifySysPage.html");
    }

    /**
     * 修改API
     */
    @Roles(roles = "system")
    public void ModifySysPageApi() {
        //取得数据
        Datas data = getDatas(getPara("postdata"));
        String page_id = data.getStr("page_id");//主键
        String actions = getStr(data, "actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysPage.dao.findById(page_id)
                    .set("page_name", data.getStr("page_name"))
                    .set("page_key", data.getStr("page_key"))
                    .set("page_desc", data.getStr("page_desc"))
                    .set("tong_tag", data.getInt("tong_tag", 0))
                    .set("cai_tag", data.getInt("cai_tag", 0))
                    .set("sindex", data.getInt("sindex"))
                    .update();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysPage()
                    .set("page_id", data.getStr("page_id"))
                    .set("page_name", data.getStr("page_name"))
                    .set("page_key", data.getStr("page_key"))
                    .set("page_desc", data.getStr("page_desc"))
                    .set("tong_tag", data.getInt("tong_tag", 0))
                    .set("cai_tag", data.getInt("cai_tag", 0))
                    .set("sindex", data.getInt("sindex"))
                    .save();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改field API
     */
    @Roles(roles = "system")
    public void ModifySysPageFieldApi() {
        int page_id = getParaToInt("page_id", 0);
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysPage.dao.findById(page_id).set(field, value).update();
        renderJson(JsonResult.n(0, "修改成功"));
    }

    /**
     * 删除接口API
     */
    @Roles(roles = "system")
    public void DelSysPageApi() {
        String page_id = getParaStr("page_id", "");
        SysPage pages = SysPage.dao.findById(page_id);
        if (pages == null) {
            renderJson(JsonResult.n(9, "记录不存在"));
        } else if (pages.delete()) {
            renderJson(JsonResult.n(0, "删除成功"));
        } else {
            renderJson(JsonResult.n(9, "删除失败"));
        }
    }

}